package com.gzw.dms.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.utils.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/27.
 */
public class OrderAdapter extends BaseAdapter {
    private Context mContext;
    private List<ShippingOrderInfo> lsInfo;
    private static HashMap<Integer, Boolean> isSelected;

    public OrderAdapter(Context _mContext,
                        List<ShippingOrderInfo> _lsInfo) {
        this.mContext = _mContext;
        this.lsInfo = _lsInfo;

        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lsInfo.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        // 加载布局文件
        final  Holder mHolder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_order, null);
            mHolder = new Holder();
            mHolder.tv_hybh = (TextView) convertView.findViewById(R.id.tv_hybh);
            mHolder.tv_dhlxr = (TextView) convertView.findViewById(R.id.tv_dhlxr);
            mHolder.tv_ztj = (TextView) convertView.findViewById(R.id.tv_ztj);
            mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);

            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }

        ShippingOrderInfo shippingOrderInfo = lsInfo.get(arg0);
        mHolder.tv_hybh.setText(shippingOrderInfo.getShiping_order_num());
        mHolder.tv_dhlxr.setText(shippingOrderInfo.getReceipt_name());
        mHolder.tv_ztj.setText(String.valueOf(shippingOrderInfo.getGoods_num()));
        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mHolder.cb.setChecked(isChecked);
                isSelected.put(arg0, isChecked);
            }
        });
        mHolder.cb.setChecked(getIsSelected().get(arg0));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        OrderAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsInfo.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsInfo.size();
    }

    public static class Holder {
        public TextView tv_hybh;
        public TextView tv_dhlxr;
        public TextView tv_ztj;
        public CheckBox cb;
    }
}